﻿using Lamex.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mIE.Common
{
    public class GetBOM_Logic
    {
        /// <summary>
        /// 计算D44特征值
        /// </summary>
        /// <param name="BOM">物料号</param>
        /// <param name="dtBOM">BOM表</param>
        /// <returns></returns>
        public static double D44_V(string BOM, DataTable dtBOM)
        {
            double result = 0;
            result = Convert.ToDouble(dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and MATERIAL_GROUP='23'").ToString().Length > 0 ?
                            dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and MATERIAL_GROUP='23'") : "0");

            return result;
        }
        /// <summary>
        /// 计算D47特征值
        /// </summary>
        /// <param name="BOM">物料号</param>
        /// <param name="Feature_Definatian">特征名称</param>
        /// <param name="dtBOM">BOM表</param>
        /// <param name="dtContrast">主数据与BOM表交集</param>
        /// <param name="dtBom_NECESSARY">物料适用表</param>
        /// <returns></returns>
        public static double D47_V(string BOM, string Feature_Definatian,DataTable dtBOM,DataTable dtContrast,DataTable dtBom_NECESSARY)
        {
            double result = 0;
            switch (Feature_Definatian)
            {
                case "D47_抽屉组装":
                    result = Convert.ToDouble(dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and MATERIAL_GROUP='23'").ToString().Length > 0 ?
                        dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and MATERIAL_GROUP='23' ") : "0");
                    break;
                case "D47_装板_W500":
                    result = Convert.ToDouble(dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and Width<500 and Length<500 and Width>0 and Length>0 ").ToString().Length > 0 ?
                        dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and Width>0 and Length>0 and Width<500 and Length<500") : "0");
                    break;
                case "D47_装板_W1300":
                    result = Convert.ToDouble(dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and Width<1300 and Length<1300 and (width>500 or Length>500)").ToString().Length > 0 ?
                        dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and  Width<1300 and Length<1300 and (width>500 or Length>500)") : "0");
                    break;
                case "D47_装板_W1301":
                    result = Convert.ToDouble(dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and (Width>=1300 or Length>=1300)").ToString().Length > 0 ?
                        dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "'  and (Width>=1300 or Length>=1300)") : "0");
                    break;
                case "D47_拧金刚针圈":
                    result = Convert.ToDouble(DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='拧金刚针圈'").CopyToDataTable()).Rows.Count > 0 ?
                        (DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='拧金刚针圈'").CopyToDataTable()).Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "'").ToString().Length > 0 ?
                        DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='拧金刚针圈'").CopyToDataTable()).Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "'") : "0") : "0");
                    break;
                case "D47_装拉门配件":
                    result = Convert.ToDouble(DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装拉门配件'").CopyToDataTable()).Rows.Count > 0 ?
                        (DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装拉门配件'").CopyToDataTable()).Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "'").ToString().Length > 0 ?
                        DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装拉门配件'").CopyToDataTable()).Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "'") : "0") : "0");
                    break;
                case "D47_装趟门配件":
                    result = Convert.ToDouble(DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装趟门配件'").CopyToDataTable()).Rows.Count > 0 ?
                        (DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装趟门配件'").CopyToDataTable()).Compute("sum(SINGLE_QTY)/2", "MATERIAL_NO='" + BOM + "'").ToString().Length > 0 ?
                        DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装趟门配件'").CopyToDataTable()).Compute("sum(SINGLE_QTY)/2", "MATERIAL_NO='" + BOM + "'") : "0") : "0");
                    break;
                case "D47_锁螺丝数量":
                    result = Convert.ToDouble(dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and MATERIAL_GROUP='46'").ToString().Length > 0 ?
                        dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and MATERIAL_GROUP='46'") : "0");
                    break;
                case "D47_配件种类":
                    result = Convert.ToDouble(dtBOM.Compute("count(MATERIAL_NO)", "MATERIAL_NO='" + BOM + "'").ToString().Length > 0 ?
                        dtBOM.Compute("count(MATERIAL_NO)", "MATERIAL_NO='" + BOM + "'") : "0");
                    break;
                case "D47_装特别配件10":
                    result = Convert.ToDouble(DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装特别配件10'").CopyToDataTable()).Rows.Count > 0 ?
                        (DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装特别配件10'").CopyToDataTable()).Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "'").ToString().Length > 0 ?
                         DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装特别配件10'").CopyToDataTable()).Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "'") : "0") : "0");
                    break;
                case "D47_装特别配件30":
                    result = Convert.ToDouble(DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装特别配件30'").CopyToDataTable()).Rows.Count > 0 ?
                        (DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装特别配件30'").CopyToDataTable()).Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "'").ToString().Length > 0 ?
                         DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装特别配件30'").CopyToDataTable()).Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "'") : "0") : "0");
                    break;
                case "D47_装特别配件50":
                    result = Convert.ToDouble(DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装特别配件50'").CopyToDataTable()).Rows.Count > 0 ?
                        (DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装特别配件50'").CopyToDataTable()).Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "'").ToString().Length > 0 ?
                         DataTableContrast(dtContrast, dtBom_NECESSARY.Select("Operation_Name='装特别配件50'").CopyToDataTable()).Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "'") : "0") : "0");
                    break;
            }
            return result;
        }

        /// <summary>
        /// 计算D51特征值
        /// </summary>
        /// <param name="BOM">物料号</param>
        /// <param name="Feature_Definatian">特征名称</param>
        /// <param name="Routing_List">工艺路线</param>
        /// <param name="dtBOM">BOM表</param>
        /// <returns></returns>
        public static double D51_V(string BOM, string Feature_Definatian,string[] Routing_List,DataTable dtBOM)
        {
            double result = 0;
            switch (Feature_Definatian)
            {
                case "D51_PE包柜/桶":
                    if ((Routing_List.Contains("D51") && dtBOM.Select("MATERIAL_NO='" + BOM + "' and COMPONENT='1990054960'").Length > 0) &&
                        (Routing_List.Contains("D44") || Routing_List.Contains("D47")))
                        result = 1;
                    break;
                case "D51_PE包板件":
                    if ((Routing_List.Contains("D51") && dtBOM.Select("MATERIAL_NO='" + BOM + "' and COMPONENT='1990054960'").Length > 0) &&
                       (!Routing_List.Contains("D44") && !Routing_List.Contains("D47")))
                        result = 1;
                    break;
                case "D51_纸箱包柜子":
                    if ((Routing_List.Contains("D51") && dtBOM.Select("MATERIAL_NO='" + BOM + "' and COMPONENT='1990054960'").Length <= 0) &&
                       (Routing_List.Contains("D44") || Routing_List.Contains("D47")))
                        result = 1;
                    break;
                case "D51_纸箱包板件":
                    if ((Routing_List.Contains("D51") && dtBOM.Select("MATERIAL_NO='" + BOM + "' and COMPONENT='1990054960'").Length <= 0) &&
                        (!Routing_List.Contains("D44") && !Routing_List.Contains("D47")))
                        // result = 1;
                        result = Convert.ToDouble(dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and Width>0 and Length>0 ").ToString().Length > 0 ?
                            dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and Width>0 and Length>0 ") : "1");                
                    break;
                //case "D51_板件数量":
                //    result = Convert.ToDouble(dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and Width>0 and Length>0 ").ToString().Length > 0 ?
                //        dtBOM.Compute("sum(SINGLE_QTY)", "MATERIAL_NO='" + BOM + "' and Width>0 and Length>0 ") : "0");
                //    break;
            }
            return result;
        }

        public static double D54_V(string BOM, DataTable dtBOM)
        {
            double result = 0;
            result = Convert.ToDouble(dtBOM.Compute("count(MATERIAL_NO)", "MATERIAL_NO='" + BOM + "'").ToString().Length > 0 ?
                                  dtBOM.Compute("count(MATERIAL_NO)", "MATERIAL_NO='" + BOM + "'") : "0");

            return result;
        }

      
        /// <summary>
        /// table交集
        /// </summary>
        /// <param name="table1"></param>
        /// <param name="table2"></param>
        /// <returns></returns>
        public static DataTable DataTableContrast(DataTable table1, DataTable table2)
        {
            DataTable dt = new DataTable();
            if (table1 != null && table2 != null)
            {
                var query = (from c in table1.Rows.Cast<DataRow>()

                             join d in table2.Rows.Cast<DataRow>() on c["COMPONENT"] equals d["Material_No"]

                             select c).ToList();
                if (query.Count > 0)
                {
                    dt = query.CopyToDataTable();
                }
            }
            return dt;

        }  

    }
}
